package com.moleq.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.base.EditableListener;
import com.moleq.mgpos.activities.R;
import com.moleq.models.RoundingSetting;

public class RoundSetupAdapter extends MQBaseAdapter {

	public static final String ROUND_EDITED = "RoundEdit";
	private LayoutInflater mInflater;
	private List<RoundingSetting> listRound;
	private EditableListener mEditableListener;
	private Context shareContext;

	public RoundSetupAdapter(Context context, List<RoundingSetting> listRound) {
		this.mInflater = LayoutInflater.from(context);
		this.shareContext = context;
		this.listRound = listRound;
	}

	public void setEditableListener(EditableListener listener) {
		mEditableListener = listener;
	}

	public void setSource(List<RoundingSetting> listRound) {
		this.listRound = listRound;
	}

	public int getCount() {
		return listRound.size();
	}

	public Object getItem(int position) {
		return listRound.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		super.getView(position, convertView, parent);
		ViewHolder holder = null;
//		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.ps_round_row, null);
			holder.tv_roundno = (TextView) convertView.findViewById(R.id.tv_roundno);
			holder.tv_roundname = (TextView) convertView.findViewById(R.id.tv_roundname);
			holder.tv_downvalue = (TextView) convertView.findViewById(R.id.tv_downvalue);
			holder.tv_upvalue = (TextView) convertView.findViewById(R.id.tv_upvalue);
			holder.tv_tovalue = (TextView) convertView.findViewById(R.id.tv_tovalue);
			holder.tv_optional = (TextView) convertView.findViewById(R.id.tv_optional);
			holder.llRoundRow = (LinearLayout) convertView.findViewById(R.id.llRoundRow);

			convertView.setTag(holder);
//		} else {
//			holder = (ViewHolder) convertView.getTag();
//		}

		holder.tv_roundno.setText(String.valueOf(position + 1));
		listRound.get(position).setNo(position + 1);

		holder.tv_roundname.setText(listRound.get(position).getRs_name());
		
		double downvalue = listRound.get(position).getRs_down_value();
		holder.tv_downvalue.setText(String.format("%.2f",downvalue));
		
		double upvalue = listRound.get(position).getRs_up_value();
		holder.tv_upvalue.setText(String.format("%.2f",upvalue));
		
		double tovalue = listRound.get(position).getRs_to_value();
		holder.tv_tovalue.setText(String.format("%.2f",tovalue));
		
		if (listRound.get(position).isRs_optional()) {
			holder.tv_optional.setText("Yes");
		}else {
			holder.tv_optional.setText("No");
		}

		if (position == selectedRow) {
			holder.llRoundRow.setBackgroundColor(Color.rgb(255, 219, 16));
		}else {
			holder.llRoundRow.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		return convertView;
	}

	@Override
	public void onTextChange(int resId, String changedText)  {
		
//		switch (resId) {
//		case R.id.et_bottledesc:
//			listBottle.get(selectedRow).setBo_desc(changedText);
//			break;
//		case R.id.et_bottleamt:
//			if (MoleQUtil.isNumeric2(changedText)) {
//				listBottle.get(selectedRow).setBo_amt(Double.valueOf(changedText));
//			}else{
//				listBottle.get(selectedRow).setBo_amt(0);
//			}
//			break;
//		}
//		listBottle.get(selectedRow).setEdit(true);
//		mEditableListener.onEdited(BOTTLE_EDITED);
	}

	public final class ViewHolder {
		public TextView tv_roundno;
		public TextView tv_roundname;
		public TextView tv_downvalue;
		public TextView tv_upvalue;
		public TextView tv_tovalue;
		public TextView tv_optional;
		private LinearLayout llRoundRow;
	}
}
